package com.xzst.jacksonxml.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * Author: MaYongLong
 * Email:  yonglong.ma.hp@gmail.com
 * Date:   2015-10-27 9:42
 * Description:Created with IntelliJ IDEA 14.1
 */
public class RandomUtil {

    private static  SimpleDateFormat sf17=new SimpleDateFormat("yyyyMMddHHmmssSSS");
    /**
     * 生成token
     *
     * @return
     */
    public static String buildToken() {
        return randomDigitalLetter(32);
    }

    /**
     * 生成appkey
     *
     * @param channelId
     * @return
     */
    public static String buildAppKey(String channelId) {
        return channelId + randomDigitalCapital(13);
    }

    /**
     * 生成appSecret
     *
     * @return
     */
    public static String buildAppSecret() {
        return randomDigitalCapital(64);
    }

    /**
     * 生成规则：channelid+yyyyMMddHHmmssSSS+四位数字大小写字母
     * 位数：3+1+17+1+4=26
     * 用途：用于填充操作流水号字段，本流水号无意义
     *
     * @return
     */
    public static String buildOprNum(String channelId, String ts) {
        return channelId + "_" + ts + "_" + randomDigitalLetter(4);
    }

    /**
     * * 位数：4+17+11=32
     * @param code
     * @return
     */
    public static String buildTransId(String code) {

        return code  + sf17.format(new Date())  + randomDigitalLetter(11);
    }

    public static String buildTransId() {

        return sf17.format(new Date())  + randomDigitalLetter(15);
    }

    /**
     * 随机生成指定长度的大小写字母
     *
     * @param length
     * @return
     */
    public static String randomLetter(int length) {
        String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(base.charAt(random.nextInt(base.length())));
        }
        return sb.toString();
    }

    /**
     * 随机生成指定长度数字字母
     *
     * @param length
     * @return
     */
    public static String randomDigitalLetter(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(base.charAt(random.nextInt(base.length())));
        }
        return sb.toString();
    }

    /**
     * 随机生成指定长度数字大写字母
     *
     * @param length
     * @return
     */
    public static String randomDigitalCapital(int length) {
        String base = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(base.charAt(random.nextInt(base.length())));
        }
        return sb.toString();
    }

    /**
     * 随机生成指定长度数字小写字母
     *
     * @param length
     * @return
     */
    public static String randomDigitalLowercase(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(base.charAt(random.nextInt(base.length())));
        }
        return sb.toString();
    }

    /**
     * 随机生成短信验证码
     *
     * @return
     */
    public static String randomSmsCode(boolean smsSendDisabled) {
        return smsSendDisabled ? "123456" : String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
    }

    /**
     * 获取随机 UUID
     *
     * @return
     */
    public static String uid() {
        return UUID.randomUUID().toString().replace("-", "");
    }


}
